import BButton from 'components/button/BButton'
import BInput from 'components/form/BInput'
import Switch from 'components/form/BSwitch'
import BRadio from 'components/radio/BRadio'
import BRadioGroup from 'components/radio/BRadioGroup'
import BCheckbox from 'components/checkbox/BCheckbox'
import BCheckboxGroup from 'components/checkbox/BCheckboxGroup'

import BAlert from 'components/alert/BAlert'
import Modal from 'components/modal/Modal'
import Notice from 'components/notice/Notice'
import BToast from 'components/toast/BToast'
import BTooltip from 'components/tooltip/BTooltip'
import BPoptip from 'components/poptip/BPoptip'
import BProgress from 'components/progress/BProgress'
import BCircle from 'components/circle/BCircle'
import BSwiper from 'components/swiper/BSwiper'
import BSwiperItem from 'components/swiperitem/BSwiperItem'
import BTag from 'components/tag/BTag'
import BAvatar from 'components/avatar/BAvatar'
import BMenu from 'components/menu/BMenu'
import BLoading from 'components/loading/BLoading'
import BScroll from 'components/scroll/BScroll'
import BList from 'components/list/BList'
import BListTitle from 'components/list/BListTitle'
import BListItem from 'components/list/BListItem'
import BTable from 'components/table/BTable'
import BTableHead from 'components/table/BTableHead'
import BTableBody from 'components/table/BTableBody'
import BTableTr from 'components/table/BTableTr'
import BTableTd from 'components/table/BTableTd'
import BMask from 'components/mask/BMask'


import './style/index.css'

import globalDirectives from './directives/'

const components = [
  BInput,
  Switch,
  BRadio,
  BRadioGroup,
  BCheckbox,
  BCheckboxGroup,
  BButton,
  BAlert,
  Modal,
  Notice,
  BToast,
  BTooltip,
  BPoptip,
  BProgress,
  BCircle,
  BSwiper,
  BSwiperItem,
  BTag,
  BAvatar,
  BMenu,
  BLoading,
  BScroll,
  BList,
  BListTitle,
  BListItem,
  BTable,
  BTableHead,
  BTableBody,
  BTableTr,
  BTableTd,
  BMask
]

function globalInstance(Vue, option) {
  const constructor = Vue.extend(option);
  const instance = new constructor();
  instance.$mount(document.createElement('div'))
  document.body.appendChild(instance.$el);
  return instance;
}

const install = function (Vue) {

  components.forEach(item => {
    Vue.component(item.name, item);
  })
  /**自定义全局实例 */
  Vue.prototype.$Toast = globalInstance(Vue, BToast);
  Vue.prototype.$Notice = globalInstance(Vue, Notice);

  /**自定义指令 */
  globalDirectives(Vue);
}

// auto install
if (typeof window !== 'undefined' && window.Vue) {
  install(window.Vue);
}

export default {
  install
}
